import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonAddBooksToFolder.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerTeacher.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/module/openLibraryReadingBody.dart';
import '../../controller/books.dart';
import '../../models/book/BookSearchDto.dart';
import '../../models/common/LevelOption.dart';
import '../../themes/pageStyles.dart';
import '../../widgets/sidebars/teacherSidebar.dart';

class OpenReadingBookForTeacherPage extends StatefulWidget {
  OpenReadingBookForTeacherPage({
    Key? key,
    this.from,
  }) : super(key: key);
  final String? from;

  //static const String routeName = '/teacher/library';
  _OpenReadingBookForTeacherPage createState() =>
      new _OpenReadingBookForTeacherPage();
}

class _OpenReadingBookForTeacherPage
    extends State<OpenReadingBookForTeacherPage> {
  final BooksController _booksController = Get.find();

  // int limit = 20;
  // int selectedPageNumber = 1;
  var box = Hive.box('appconfig');
  late String classId;

  @override
  void initState() {
    _booksController.setBookSearchDto(new BookSearchDto());
    _booksController.openLibraryReadingBookItems.clear();
    _booksController.selectedLibraryReadingBookItemsClear();
    classId = box.get(CacheManagerKey['currentclass'].toString());
    // Future.delayed(Duration(milliseconds: 200), () {
    //   _booksController.selectedLibraryReadingBookItemsClear();
    //   _booksController.fetchOpenReadingItems(limit, selectedPageNumber - 1);
    // });
    super.initState();
  }

  @override
  void dispose() {
    List<LevelOption> levelOptions = [];
    _booksController.setLevelOptions(levelOptions);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build OpenReadingBookForTeacherPage');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.teacherPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    if (widget.from == 'assignment') {
      return Scaffold(
          drawer: TeacherAppDrawer(),
          body: OpenLibraryReadingBody(),
          floatingActionButton: FloatingActionButtonAddBookToFolder(
            readingType: NanhaiBookReadingTypes.library,
          ));
    } else {
      return Scaffold(
          backgroundColor: Colors.transparent,
          appBar: isSmallScreen ? CustomAppBar(title: 'Library'.tr) : null,
          drawer: isSmallScreen ? TeacherAppDrawer() : null,
          body: Container(
            decoration: _pageStyles.pageBoxDecoration(),
            child: Row(
              children: [
                Visibility(
                    visible: !isSmallScreen,
                    child: TeacherSidebarX(
                      selectedIndex: _selectedSidebarItem,
                      isBigScreen: isBigScreen,
                    )),
                Expanded(
                  child: Container(
                    margin: _pageStyles.mainContainerWithPaginationEdgeInsets(),
                    child: OpenLibraryReadingBody(),

                    // Column(
                    //   crossAxisAlignment: CrossAxisAlignment.start,
                    //   mainAxisAlignment: MainAxisAlignment.start,
                    //   children: [
                    //     //椭圆形级数选项背景
                    //     Container(
                    //         margin: EdgeInsets.only(bottom: 12.0),
                    //         padding: EdgeInsets.only(left: 6.0),
                    //         decoration: BoxDecoration(
                    //             borderRadius: BorderRadius.circular(3.0),
                    //             color: Get.isDarkMode
                    //                 ? AppTheme.colors.darkTheme2
                    //                 : Colors.white.withOpacity(0.3)),
                    //         child: LevelChioceChipWidget(pagingKey: paginationKey)),
                    //     Flexible(
                    //       child: Center(
                    //         child: _booksController.obx(
                    //           (data) => BookGridWithCheckBoxTeaching(
                    //             books: _booksController
                    //                 .openLibraryReadingBookItems,
                    //             readingType: NanhaiBookReadingTypes.library,
                    //           ),
                    //           onEmpty: Text('Books not available'.tr),
                    //           onLoading: Container(),
                    //           // onLoading: Center(
                    //           //     child: CircularProgressIndicator(
                    //           //   backgroundColor: Get.theme.primaryColorDark,
                    //           //   strokeWidth: 9.0,
                    //           // )),
                    //         ),
                    //       ),
                    //     ),
                    //     Padding(
                    //       padding: const EdgeInsets.only(top: 12.0),
                    //       child: Center(
                    //         child: _booksController.obx(
                    //           (data) => NumberPagination(
                    //             key: paginationKey,
                    //             onPageChanged: (int pageNumber) {
                    //               //got new books for selected page
                    //               setState(() {
                    //                 selectedPageNumber = pageNumber;
                    //                 _booksController.fetchOpenReadingItems(
                    //                     limit, pageNumber - 1);
                    //               });
                    //             },
                    //             pageTotal: _booksController
                    //                             .libraryBooksTotalNum.value %
                    //                         limit ==
                    //                     0
                    //                 ? _booksController
                    //                         .libraryBooksTotalNum.value ~/
                    //                     limit
                    //                 : _booksController
                    //                             .libraryBooksTotalNum.value ~/
                    //                         limit +
                    //                     1,
                    //             threshold: 5,
                    //             pageInit: selectedPageNumber,
                    //             // picked number when init page
                    //             colorPrimary:
                    //                 Theme.of(context).primaryColorDark,
                    //             colorSub: Get.isDarkMode
                    //                 ? AppTheme.colors.darkTheme3
                    //                 : Colors.white,
                    //           ),
                    //         ),
                    //       ),
                    //     ),
                    //   ],
                    // ),
                  ),
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButtonAddBookToFolder(
              readingType: NanhaiBookReadingTypes.library));
    }
  }
}
